package com.liao.starapp.partner;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.liao.starapp.R;
import com.liao.starapp.luck.Luck;
import com.liao.starapp.utils.GetAssetsData;
import com.liao.starapp.views.CircleImageView1;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class PartnerActivity extends AppCompatActivity implements View.OnClickListener {

    private Luck boy;
    private Luck girl;
    private CircleImageView1 img_boy;
    private CircleImageView1 img_girl;
    private TextView tv_starBoyName;
    private TextView tv_starGirlName;
    private TextView tv_star;
    private TextView tv_star2;
    private TextView tv_zhishu;
    private TextView tv_jieguo;
    private TextView tv_advise;
    private TextView tv_zhuyi;
    private TextView tv_bizhong;

    private ProgressDialog progressDialog;

    private String data;

    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            progressDialog.dismiss();
            json(data);
            return false;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_partner);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        getWindow().setStatusBarColor(getColor(R.color.actionBarColor));
        Intent intent = getIntent();
        boy = (Luck) intent.getSerializableExtra("boy");
        girl = (Luck) intent.getSerializableExtra("girl");
        initView();
    }

    private void initView() {
        img_boy = (CircleImageView1) findViewById(R.id.img_boy);
        img_girl = (CircleImageView1) findViewById(R.id.img_girl);
        tv_starBoyName = (TextView) findViewById(R.id.tv_starBoyName);
        tv_starGirlName = (TextView) findViewById(R.id.tv_starGirlName);
        tv_star = (TextView) findViewById(R.id.tv_star);
        tv_star2 = (TextView) findViewById(R.id.tv_star2);
        tv_zhishu = (TextView) findViewById(R.id.tv_zhishu);
        tv_jieguo = (TextView) findViewById(R.id.tv_jieguo);
        tv_advise = (TextView) findViewById(R.id.tv_advise);
        tv_zhuyi = (TextView) findViewById(R.id.tv_zhuyi);
        tv_bizhong = (TextView) findViewById(R.id.tv_bizhong);
        findViewById(R.id.img_back).setOnClickListener(this);
        TextView title = findViewById(R.id.tv_title);
        title.setText("配对分析");
        alertLoadDialog();
        new MyThread().start();
    }

    private void alertLoadDialog() {
        progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("正在加载...");
        progressDialog.setCancelable(false);
        progressDialog.show();
    }

    @Override
    public void onClick(View v) {
        onBackPressed();
        finish();
    }

    private class MyThread extends Thread {
        @Override
        public void run() {
            getData();
        }
    }

    private void getData() {
        try {
            String text = "http://apis.juhe.cn/xzpd/query?men=" + boy.getName().substring(0, 2) +
                    "&women=" + girl.getName().substring(0, 2) + "&key=7034dae5e39c547984ddfc2a8bed7442";
            System.out.println("====" + text);
            URL url = new URL(text);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            InputStream is = connection.getInputStream();
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            int length;
            byte[] bytes = new byte[1024];
            while ((length = is.read(bytes)) != -1) {
                os.write(bytes, 0, length);
            }
            data = os.toString();
            is.close();
            handler.sendEmptyMessage(0x000);
        } catch (IOException e) {
            progressDialog.dismiss();
            Toast.makeText(this, "出了点小问题...", Toast.LENGTH_SHORT).show();
        }
    }

    private void json(String s) {
        try {
            JSONObject jsonObject1 = new JSONObject(s);
            JSONObject jsonObject = jsonObject1.getJSONObject("result");
            Partner partner = new Partner();
            partner.setMen(jsonObject.getString("men"));
            partner.setWomen(jsonObject.getString("women"));
            partner.setZhishu(jsonObject.getString("zhishu"));
            partner.setBizhong(jsonObject.getString("bizhong"));
            partner.setXiangyue(jsonObject.getString("xiangyue"));
            partner.setTcdj(jsonObject.getString("tcdj"));
            partner.setJieguo(jsonObject.getString("jieguo"));
            partner.setLianai(jsonObject.getString("lianai"));
            partner.setZhuyi(jsonObject.getString("zhuyi"));
            setData(partner);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @SuppressLint("SetTextI18n")
    private void setData(Partner partner) {
        String boyName = boy.getName();
        String girlName = girl.getName();
        tv_starBoyName.setText(boyName);
        tv_starGirlName.setText(girlName);
        Bitmap bitmapBoy = GetAssetsData.getGridViewImg(this, "xzcontentlogo/" + boy.getLogoname() + ".png");
        Bitmap bitmapGirl = GetAssetsData.getGridViewImg(this, "xzcontentlogo/" + girl.getLogoname() + ".png");
        img_boy.setImageBitmap(bitmapBoy);
        img_girl.setImageBitmap(bitmapGirl);
        String star = "星座配对 - " + boyName + "和" + girlName + "配对";
        String star2 = boyName.substring(0, 2) + " VS " + girlName.substring(0, 2);
        tv_star.setText(star);
        tv_star2.setText(star2);

        tv_zhishu.setText("配对评分：" + partner.getZhishu());
        tv_bizhong.setText("星座比重：" + partner.getBizhong());
        tv_jieguo.setText(partner.getJieguo());
        tv_advise.setText(partner.getLianai());
        tv_zhuyi.setText(partner.getZhuyi());
    }
}
